package cn._51doit.flink.day04;

import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.ProcessingTimeSessionWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

/**
 * 按照 processing time 划分会话窗口（按照时间间隔划分窗口）
 * 不keyBy，只会划分nonKeyed Window（window算子对应的DataStream只有一个并行）
 */
public class ProcessingSessionWindowAllDemo {

    public static void main(String[] args) throws Exception {

        //没有keyBy的窗口： non keyed window
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //1
        //2
        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);

        SingleOutputStreamOperator<Integer> nums = lines.map(e -> Integer.parseInt(e));

        AllWindowedStream<Integer, TimeWindow> window = nums.windowAll(ProcessingTimeSessionWindows.withGap(Time.seconds(5)));

        //对创建中的数据进行增量聚合
        SingleOutputStreamOperator<Integer> res = window.sum(0);

        res.print();

        env.execute();
    }
}
